# Maintainer: Martell Malone <martellmalone@gmail.com>
# Maintainer: Ray Donnelly <mingw.android@gmail.com>
# Contributor: David Macek <david.macek.0@gmail.com>
# Contributor: Mateusz Mikuła <mati865@gmail.com>
# Contributor: Alethea Rose <alethea@alethearose.com>

_realname=nodejs
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=8.12.0
pkgrel=1
pkgdesc="Evented I/O for V8 javascript (mingw-w64)"
arch=('any')
url="https://nodejs.org/"
license=("MIT")
makedepends=("${MINGW_PACKAGE_PREFIX}-python2" 'make')
depends=("${MINGW_PACKAGE_PREFIX}-c-ares"
         "${MINGW_PACKAGE_PREFIX}-http-parser"
         "${MINGW_PACKAGE_PREFIX}-icu"
         "${MINGW_PACKAGE_PREFIX}-libuv"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "winpty")
optdepends=("${MINGW_PACKAGE_PREFIX}-npm: nodejs package manager")
options=('!emptydirs' '!strip') # 'debug')

source=("https://nodejs.org/dist/v${pkgver}/node-v${pkgver}.tar.xz"
        0001-Fix-python-invocation-to-python2.patch
        0002-Fix-system-icu-build.patch
        0003-Include-win32-headers.h-in-v8-atomicops.h.patch
        0004-Define-localtime_s-for-MinGW.patch
        0005-Remove-.lib-suffix-on-linked-libraries.patch
        0006-Define-_WIN32_WINNT-in-node.gyp.patch
        0007-Skip-sys-resource-and-unneeded-includes-in-node.cc.patch
        0008-Link-with-municode-and-extern-wmain-for-Unicode.patch
        0009-Remove-no_rand_screen-from-s_client-invocation.patch
        0010-Fix-incorrect-test-assumptions-for-MinGW.patch
        0011-detect-architecture.patch
        0012-Use-shell-wrapper-script-for-npm-on-MSYS2-mingw-w64.patch
        0013-Revert-to-FHS-installation-paths-on-Windows-tests-no.patch
        0014-cctest-allow-building-with-municode.patch
        0015-format-macros.h-do-not-rely-on-__STDC_FORMAT_MACROS.patch
        0016-Do-not-get-confused-by-the-presence-of-winpthread.patch
        0017-Add-missing-include-to-build-condition-variable.cc-s.patch
        0018-inspector-use-namespaced-icu-symbols.patch
        0019-Use-wide-character-Win32-API-functions-to-handle-wch.patch
        0020-Disable-MSVC_HACK.patch
        0021-Make-sure-that-__rdtsc-is-declared.patch
        0022-win32-avoid-including-non-existing-headers.patch
        0023-win32-work-around-stat-fstat-defines.patch
        0024-win32-work-around-path-issues.patch
        'node')

prepare() {
  cd "${srcdir}/node-v${pkgver}"

  patch -Np1 -i "${srcdir}"/0001-Fix-python-invocation-to-python2.patch
  patch -Np1 -i "${srcdir}"/0002-Fix-system-icu-build.patch
  patch -Np1 -i "${srcdir}"/0003-Include-win32-headers.h-in-v8-atomicops.h.patch
  patch -Np1 -i "${srcdir}"/0004-Define-localtime_s-for-MinGW.patch
  patch -Np1 -i "${srcdir}"/0005-Remove-.lib-suffix-on-linked-libraries.patch
  patch -Np1 -i "${srcdir}"/0006-Define-_WIN32_WINNT-in-node.gyp.patch
  patch -Np1 -i "${srcdir}"/0007-Skip-sys-resource-and-unneeded-includes-in-node.cc.patch
  patch -Np1 -i "${srcdir}"/0008-Link-with-municode-and-extern-wmain-for-Unicode.patch
  patch -Np1 -i "${srcdir}"/0009-Remove-no_rand_screen-from-s_client-invocation.patch
  patch -Np1 -i "${srcdir}"/0010-Fix-incorrect-test-assumptions-for-MinGW.patch
  patch -Np1 -i "${srcdir}"/0011-detect-architecture.patch
  patch -Np1 -i "${srcdir}"/0012-Use-shell-wrapper-script-for-npm-on-MSYS2-mingw-w64.patch
  patch -Np1 -i "${srcdir}"/0013-Revert-to-FHS-installation-paths-on-Windows-tests-no.patch
  patch -Np1 -i "${srcdir}"/0014-cctest-allow-building-with-municode.patch
  patch -Np1 -i "${srcdir}"/0015-format-macros.h-do-not-rely-on-__STDC_FORMAT_MACROS.patch
  patch -Np1 -i "${srcdir}"/0016-Do-not-get-confused-by-the-presence-of-winpthread.patch
  patch -Np1 -i "${srcdir}"/0017-Add-missing-include-to-build-condition-variable.cc-s.patch
  patch -Np1 -i "${srcdir}"/0018-inspector-use-namespaced-icu-symbols.patch
  patch -Np1 -i "${srcdir}"/0019-Use-wide-character-Win32-API-functions-to-handle-wch.patch
  patch -Np1 -i "${srcdir}"/0020-Disable-MSVC_HACK.patch
  patch -Np1 -i "${srcdir}"/0021-Make-sure-that-__rdtsc-is-declared.patch
  patch -Np1 -i "${srcdir}"/0022-win32-avoid-including-non-existing-headers.patch
  patch -Np1 -i "${srcdir}"/0023-win32-work-around-stat-fstat-defines.patch
  patch -Np1 -i "${srcdir}"/0024-win32-work-around-path-issues.patch
}

build() {

  local -a extra_config

  cd "${srcdir}"
  sleep 5

  [[ -d ${CARCH} ]] && rm -rf ${CARCH}
  mv node-v${pkgver} ${CARCH}
  cd ${CARCH}

  if check_option "debug" "y"; then
    extra_config+=( --debug )
  fi

  local opt_cpu="x64"
  if [ "${CARCH}" = "i686" ]; then
    opt_cpu="x86"
  fi

  ./configure \
    --prefix="${MINGW_PREFIX}" \
    --dest-os=win \
    --dest-cpu=${opt_cpu} \
    --shared-openssl \
    --shared-zlib \
    --shared-cares \
    --with-intl=system-icu \
    --shared-http-parser \
    --without-perfctr \
    --without-etw \
    "${extra_config[@]}"

  ${MINGW_PREFIX}/bin/python2 tools/gyp_node.py -f make
  touch config.gypi

  # Work around the problem where the protocol files would be generated into a
  # subdirectory relative to deps/v8/src/inspector instead of relative to the
  # top-level directory.
  CXXFLAGS=-D_WIN32_WINNT=0x0600 make out/Release/obj/gen/src/inspector/protocol/Forward.h
  sed -i 's/^\(LDFLAGS_[^ ]* := \)\([^-]\)/\1-municode \2/' node.target.mk
  CXXFLAGS=-D_WIN32_WINNT=0x0600 make

  # flatten the nested node_modules/ hierarchy
  test -z "$(find deps/npm/node_modules | grep '^.\{140,\}$')" ||
  (cd deps/npm &&
   cp -R . ../npm.bup &&
   rm -rf node_modules &&
   ../../out/Release/node.exe ../npm.bup/bin/npm-cli.js install --production)
}

check() {
  cd "${srcdir}/${CARCH}"

  # A few tests still fail due to a bug with pausing/resuming file streams
  ${MINGW_PREFIX}/bin/python2 tools/test.py --mode=release -J \
    cctest doctool inspector message parallel sequential tick-processor || true
}

package() {
  cd "${srcdir}/${CARCH}"

  ${MINGW_PREFIX}/bin/python2 tools/install.py install "${pkgdir}" "/${MINGW_PREFIX}"

  install -d "${pkgdir}${MINGW_PREFIX}/share/doc/nodejs"
  cp -r doc/api/{*.html,assets} \
    "${pkgdir}${MINGW_PREFIX}/share/doc/nodejs"

  install -D -m644 LICENSE \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/nodejs/LICENSE"

  mv -v "${pkgdir}$MINGW_PREFIX/bin/node"{.exe,_exe}
  install -m755 "${srcdir}/node" "${pkgdir}${MINGW_PREFIX}/bin/node"
  mv -v "${pkgdir}$MINGW_PREFIX/bin/node"{_exe,.exe}

  install -m755 "${srcdir}/${CARCH}/deps/npm/bin/npm.cmd" "${pkgdir}${MINGW_PREFIX}/bin/npm.cmd"
}

sha256sums=('5a9dff58016c18fb4bf902d963b124ff058a550ebcd9840c677757387bce419a'
            'bb6da578324d36730b1b3ef5bc9a463db25d5e4407645ad34a7618f1cfae0088'
            '202d36ab36584660e06619d4e1b0afdb0482db053a9a869adcf37fa2c8b6c1d0'
            'cdded911888d3553a4666493b97c0bb71030294dbe13b874c46a2048e5c1cf9c'
            '89c72b5f8f1ee4838416e269968c816234cb62591a3d7772fc7d633f11c2acbb'
            '33982f769bc515c026f0e2ec5bc4dacecde7b12e584419b7d8ad078b4f0f8536'
            'a46689016092d4029093b27f0dfb324fa63ab2f7b0dae4e4519e6a7f722b5e25'
            '477745f1fcddd36db62aca0083441064a52bb255d8a8ca12b224678d94c29a91'
            'afed17a81d127949f287b7421b349d6e3d2ca5ed3638806580d4283c7c22f5a4'
            '6219a27adb1e76f06594f7174e16287f569c8ac1e50610a436f32a088a8d85fb'
            '8a8b25d298e803661256e0709c09bb08ea5ef8676081f95eb4faaba013bd6885'
            'afcbf7919ea1512b8ad4935d5173a57d602aae0a8c189378a13681b902bca6b8'
            '82fd8d3eea9e3a9f5efa346e79ff83e3d18a407016db9df20eddf7fdb5be3b5d'
            '60a6fde2dbf6e6e2380d2383e7503dcb238a807a1a60f0fff2836d698247799f'
            'af3289a18a339a4b8ac96b0016e173feb6941a8df1a33ae8d0caf5d7a9578030'
            'db0cf91e6d23f6a15d3a7c24f4d764a7dd3b93cfb16032dc0e5168f35114b67c'
            '89ec47f45363cd384242eaa8f521aff726d1eea4fe5e025fee01a69625bb8022'
            '9a8f136f93234dc527305b84f9692768aa5f2d091c8437ec57be0e532db0e859'
            '9d258dbdfa71e7757db14ef94584e3f22169372714fec74e446aae2e62131528'
            '18020799de0f0668f1ce3f86cd0b6a18f6164e71fc19770d7add310d58e83df4'
            '11b018a9079222c2a705e6be0e4dbce3a78f1c3e14ccd455a5718e439b1ea651'
            '358ea0bbec0ff8eb37f29d25685ecc3f23f87cb95ec18a5c6aea82f92a9c4be5'
            'ef6c3ef3abf5b3cae012e60093acb79f892fb43009f3a1b3bb7c5e1279a0963b'
            '3f0c6911c9f551823d1e0175b7ee66eb02ed79154ffc81741440cc34cbb85005'
            '55b7be4c57f6681b7768ae73ce28e5e60819c669a3edb6b8ece627bd89cbfe1a'
            '284251ff443506cd61530a026afb421d0084e4ad25c037bcfe3a045246d729a2')
